package com.meij.adapter;

import java.util.ArrayList;

import org.lucasr.twowayview.widget.StaggeredGridLayoutManager;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.meij.MJApplaction;
import com.meij.R;
import com.meij.bean.Album;
import com.meij.http.Url;
import com.util.DensityUtil;

public class PicListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

	private final Context context;
	private ArrayList<Album> albums = new ArrayList<Album>();
	private int width;
	private static final int TAKE = 0, PIC = 1;
	private String id;

	public PicListAdapter(Context context, ArrayList<Album> albums, String id) {
		this.context = context;
		this.id = id;
		this.albums = albums;
		width = (DensityUtil.getDisplay(context).widthPixels - DensityUtil.dip2px(context, 16 * 3)) / 2;
	}

	/***
	 * 查看的用户图片列表是否为当前用户的
	 */
	public boolean isCurrentUser() {
		return id.equals(MJApplaction.userInfo.getUserkey());
	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0 && isCurrentUser()) {
			return TAKE;
		} else {
			return PIC;
		}
	}

	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view;
		switch (viewType) {
		case TAKE:
			view = LayoutInflater.from(context).inflate(R.layout.item_takephoto, parent, false);
			return new TakeViewHolder(view);
		default:
			view = LayoutInflater.from(context).inflate(R.layout.item_pic, parent, false);
			return new PicViewHolder(view);
		}
	}

	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
		View itemView = holder.itemView;
		StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) itemView.getLayoutParams();
		lp.span = 1;
		if (isCurrentUser()) {
			if (position == 0) {
				double ratio = width / 330.0;
				lp.height = (int) (ratio * 215.0 * (position + 1));
			} else {
				PicViewHolder picHolder = (PicViewHolder) holder;
				Album album = albums.get(position - 1);
				AQuery aq = new AQuery(picHolder.iv_pic);
				double ratio = width / album.getWidth();
				int height = (int) (album.getHeight() * ratio);
				aq.image(Url.ctx + album.getImg()).width(width, false).height(height, false);
				picHolder.tv_date.setText(album.getCreatedTime());
				picHolder.tv_views.setText(album.getClickNum() + "");
				lp.height = height + DensityUtil.dip2px(context, 32);
			}
		} else {
			PicViewHolder picHolder = (PicViewHolder) holder;
			Album album = albums.get(position);
			AQuery aq = new AQuery(picHolder.iv_pic);
			double ratio = width / album.getWidth();
			int height = (int) (album.getHeight() * ratio);
			aq.image(Url.ctx + album.getImg()).width(width, false).height(height, false);
			picHolder.tv_date.setText(album.getCreatedTime());
			picHolder.tv_views.setText(album.getClickNum() + "");
			lp.height = height + DensityUtil.dip2px(context, 32);
		}
		itemView.setLayoutParams(lp);
	}

	@Override
	public int getItemCount() {
		int count = (albums != null ? albums.size() : 0);
		if (isCurrentUser()) {
			count = count + 1;
		}
		return count;
	}

	public class TakeViewHolder extends RecyclerView.ViewHolder {
		public TakeViewHolder(View view) {
			super(view);
		}
	}

	public class PicViewHolder extends RecyclerView.ViewHolder {
		public TextView tv_date;
		public ImageView iv_pic;
		public TextView tv_views;

		public PicViewHolder(View view) {
			super(view);
			tv_date = (TextView) view.findViewById(R.id.tv_date);
			iv_pic = (ImageView) view.findViewById(R.id.iv_pic);
			tv_views = (TextView) view.findViewById(R.id.tv_views);
		}
	}
}
